package com.bjsxt.userscenter.common.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.util.DateFormatUtil;
import com.bjsxt.userscenter.common.util.FileUtil;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.common.util.SpringEmailUtil;
import com.bjsxt.userscenter.common.util.VerifyCodeUtils;

/**
 * 所有Controller的父类
 * 
 * @author wangshSxt
 *
 */
public class BaseController
{
	@Resource
	protected DateFormatUtil dateFormatUtil ; 
	@Resource
	protected FileUtil fileUtil;
	@Resource
	protected SpringEmailUtil springEmailUtil;
	
	/* 提示信息 */
	protected String info;

	/**
	 * 生成验证码
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/randImg")
	public String randImg(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/jpeg");

		// 生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		// 存入会话session
		HttpSession session = request.getSession(true);
		session.setAttribute("rand", verifyCode.toLowerCase());
		// 生成图片
		int w = 200, h = 80;
		VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
		return null ; 
	}
	
	/**
	 * 从Request中封装分页信息
	 * 所有的列表页面都要分页
	 * @param request
	 * @return
	 */
	protected PageInfoUtil proccedPageInfo(HttpServletRequest request)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		try
		{
			String currentPage = request.getParameter("currentPage");
			String pageSize = request.getParameter("pageSize");
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (Exception e)
		{
		}
		return pageInfoUtil;
	}
	
	/**
	 * 拼装J-ui需要的JSON字符串
	 * @return
	 */
	protected JSONObject printJUIString(HttpServletRequest request,String info)
	{
		/* 从请求头中获取,将来有可能要修改 */
		String navTabId = request.getParameter("navTabId");
		String rel = request.getParameter("rel");
		String callbackType = request.getParameter("callbackType");
		String forwardUrl = request.getParameter("forwardUrl");
		String confirmMsg = request.getParameter("confirmMsg");
		if(navTabId == null)
		{
			navTabId = "" ; 
		}
		if(rel == null)
		{
			rel = "" ; 
		}
		if(callbackType == null)
		{
			callbackType = "" ; 
		}
		if(forwardUrl == null)
		{
			forwardUrl = "" ; 
		}
		if(confirmMsg == null)
		{
			confirmMsg = "" ; 
		}
		
		JSONObject resultJSON = new JSONObject() ; 
		resultJSON.put("statusCode", "200");
		resultJSON.put("message", info);
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("confirmMsg", confirmMsg);
		return resultJSON ; 
	}
}
